<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <input type="hidden" name="pid" value="{$find.pid}">
    <div class="layui-form-item">
        <label class="layui-form-label">提供礼品人</label>
        <div class="layui-input-inline">
            <select name="uid" lay-verify="required" lay-search>
                <option value="">可直接搜索</option>
                <option value="0" {eq name='find.uid' value='0'}selected{/eq}>平台</option>
                {volist name='user' id='vo'}
                <option value="{$vo.id}" {eq name='find.uid' value='$vo.id'}selected{/eq}>{$vo.name}({$vo.phone})</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">领取方式</label>
        <div class="layui-input-inline">
            <select name="mode" lay-verify="required" lay-filter="mode">
                <option value="">请选择领取方式</option>
                <option value="0" {eq name='find.mode' value='0'}selected{/eq}>到店免费领取</option>
                <option value="1" {eq name='find.mode' value='1'}selected{/eq}>到店消费领取</option>
                <option value="2" {eq name='find.mode' value='2'}selected{/eq}>线上充值领取</option>
                <option value="3" {eq name='find.mode' value='3'}selected{/eq}>线上免费领取</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-hide product">
        <label class="layui-form-label" style="padding: 0 15px;">选取商品作为礼品</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" value="{$find.name}" ts-selected="{$find.pid}" id="demo" placeholder="请选择商品" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">礼品名称</label>
        <div class="layui-input-inline">
            <input type="text" name="lipin" value="{$find.lipin}" lay-verify="required" placeholder="请输入礼品名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">礼品封面</label>
        <div class="layui-input-inline">
            <input type="text" name="img" value="{$find.img}" lay-verify="required" placeholder="请上传礼品封面" autocomplete="off" class="layui-input">
        </div>
        <button style="float: left;" type="button" class="layui-btn" id="up">上传封面</button>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">礼品数量</label>
        <div class="layui-input-inline">
            <input type="number" name="num" value="{$find.num}" lay-verify="required|number" placeholder="请输入礼品数量" autocomplete="off" class="layui-input">
        </div>
        <div style="line-height: 35px;color: #8e8e8e;">(每期礼品数量)</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">期数</label>
        <div class="layui-input-inline">
            <input type="number" name="stage" value="{$find.stage}" lay-verify="required|number" placeholder="请输入期数" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">每期间隔</label>
        <div class="layui-input-inline">
            <input type="number" name="ge" value="{$find.ge}" lay-verify="required|number" placeholder="请输入每期领取间隔天数" autocomplete="off" class="layui-input">
        </div>
        <div style="line-height: 35px;color: #8e8e8e;">(每期领取间隔天数)</div>
    </div>
    <div class="layui-form-item money">
        <label class="layui-form-label">消费金额</label>
        <div class="layui-input-inline">
            <input type="number" name="money" value="{$find.money}" placeholder="需要消费金额" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">礼品价值</label>
        <div class="layui-input-inline">
            <input type="number" name="price" value="{$find.price}" lay-verify="required|number" placeholder="礼品价值" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商家地址</label>
        <div class="layui-input-inline" style="width: 400px;">
            <textarea name="address" placeholder="请输入商家地址(线上充值可不输入)" class="layui-textarea">{$find.address}</textarea>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="提交">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="修改">
    </div>
    <script type="text/html" id="name">
        <div style="display: flex">
            <img class="imgs" src="/uploads/{{d.img}}" title="{{d.name}}" style="width: 60px;height: 60px;cursor: pointer;">
            <div style="margin-left: 15px;">
                <div style="flex-wrap: wrap;idth: 300px;">{{d.name}}</div>
                <div style="color: #f60;">¥ {{d.price}}</div>
            </div>
        </div>
    </script>
    <script type="text/html" id="time">
        {{ formatDateTime(d.time) }}
    </script>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'upload', 'form', 'tableSelect'], function(){
        var $ = layui.$,
                form = layui.form,
                upload = layui.upload,
                tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#demo',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索商品名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('Product/product')}",
                method: 'post',
                cols: [[
                    {type: 'radio'},
                    {field: 'name', title: '商品名称', width: 400, templet: "#name"},
                    {field: 'stock', title: '库存', width: 100},
                    {field: 'volume', title: '销量', width: 100},
                    {field: 'time', title: '添加时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                $("input[name=pid]").val(field[0].id);
                $("#demo").val(field[0].name);
            }
        });

        //普通图片上传
        upload.render({
            elem: '#up'
            ,url: "{:url('User/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("input[name=img]").val(res.img);
            }
        });

        var mode = "{$find.mode}";
        if (mode == 2 || mode == 3){
            $(".product").removeClass('layui-hide');
            $(".money").addClass('layui-hide');
        }else{
            $(".product").addClass('layui-hide');
            $(".money").removeClass('layui-hide');
        }

        //监听选择
        form.on('select(mode)', function(data){
            var that = data.othis;
            //选中线上充值领取
            if (data.value == 2 || data.value == 3){
                $(".product").removeClass('layui-hide');
                $(".money").addClass('layui-hide');
            }else{
                $(".product").addClass('layui-hide');
                $(".money").removeClass('layui-hide');
            }
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Gift/gift_add')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('添加成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('添加失败', {offset: ['30%', '40%']});
                    }
                }
            });
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Gift/gift_edit')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('修改成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('修改失败', {offset: ['30%', '40%']});
                    }
                }
            });
        });
    })

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }
</script>
</body>
</html>